package com.jiayi.microservice.service;

import com.jiayi.microservice.pojo.Item;
import com.jiayi.microservice.pojo.Order;
import com.jiayi.microservice.pojo.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: CONCENTRATE
 * @time: 2018/11/3.
 */
@Service
public class OrderService {

    @Autowired
    private ItemService itemService;

    private static final Map<String, Order> ORDER_MAP = new HashMap<>(5);

    static {
        // construct order data
        Order order = new Order();
        order.setOrderId("123456");
        order.setUserId(001L);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());

        //create List<OrderDetail>
        List<OrderDetail> orderDetails = new ArrayList<>();

//        Item item1 = new Item(1L,"title1", "http://pic/1", 100.00);
//        Item item2 = new Item(2L,"title2", "http://pic/2", 200.00);
        Item item1 = new Item(1L,"title1", "http://pic/111", 111.00);
        Item item2 = new Item(2L,"title2", "http://pic/222", 222.00);
        OrderDetail orderDetail1 = new OrderDetail(order.getOrderId(), item1);
        OrderDetail orderDetail2 = new OrderDetail(order.getOrderId(), item2);
        orderDetails.add(orderDetail1);
        orderDetails.add(orderDetail2);
        order.setOrderDetailList(orderDetails);

        ORDER_MAP.put(order.getOrderId(), order);
    }

    public Order queryOrderById(String orderId){
        Order order = ORDER_MAP.get(orderId);

        List<OrderDetail> orderDetailList = order.getOrderDetailList();
        for (OrderDetail orderDetail : orderDetailList) {
            Long id = orderDetail.getItem().getId();
            Item item = itemService.queryItemById(id);
            orderDetail.setItem(item);
        }
        return order;
    }
}
